<template>
  <div class="OfficialPlayList">
    <h2 id="title">达人歌单</h2>
    <div class="wrapper" ref="wrapper">
      <ul class="content" ref="content">
        <li v-for="item in data" :key="item.content_id" @click="selectPlayList(item)">
          <div class="play-img">
            <img :src="item.cover" />
            <div class="play-num">
              <img src="">
              <span>{{toFixed((item.listen_num/10000),1)}}万</span>
            </div>
          </div>
          <div class="play-bd">
            <p>{{item.title}}</p>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
import axios from "axios";
export default {
  data() {
    return {
      data: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      axios
        .get("/api/recommend/playlist/u", "")
        .then(res => {
          // console.log(res.data.data);
          this.data = res.data.data.list;
          this.betterScrollHorizontal(this.data.length, 130);
        })
        .catch(err => {
          console.error(err);
        });
    },
    betterScrollHorizontal(num, itemWidth) {
      let width = num * itemWidth;
      this.$refs.content.style.width = width + "px";
      this.$nextTick(() => {
        this.scroll = new BScroll(this.$refs.wrapper, {
          click: true,
          startX: 0,
          scrollX: true, //横向
          scrollY: false,
          freeScroll: true
        });
      });
    },
    toFixed(number, fractionDigits) {
      //number 传入数字
      //fractionDigits 保留小数位数
      var times = Math.pow(10, fractionDigits);
      var roundNum = Math.round(number * times) / times;
      return roundNum.toFixed(fractionDigits);
    },
    selectPlayList(item){
      const id = item.content_id;
      this.$router.push({
        name: 'PlayListDetail',
        params: {id},
      })
    }
  }
};
</script>
<style scoped>
.OfficialPlayList{
  background-color: #fafafa;
  padding-bottom: 15px;
}
/*父容器必须要设置横向的宽度，并且要设置子容器（内容容器）的超出部分隐藏*/
.wrapper {
  overflow: hidden;
  /* width: 100%; */
}
#title {
  font-size: 20px;
  text-align: left;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0;
  padding: 10px 15px;
  background-color: #fafafa;
}
.play-bd{
  float: left;
  margin-top: -15px;
}
.play-bd p {
  font-size: 13px;
  color: #1a1a1a;
  margin: 0;
  text-align: left;
  padding-right: 10px;
}
.play-img {
  height: 120px;
}
.play-img img {
  height: 120px;
  border-radius: 10px;
}
.play-num {
  height: 20px;
  line-height: 20px;
  position: relative;
  bottom: 24px;
  font-size: 12px;
  text-align: center;
  float: right;
  color: #fff;
  background: rgba(0, 0, 0, 0.4);
  padding: 0px 10px;
  border-radius: 10px;
  margin-right: 10px;
}
.play-num img{
  height: 10px;
  vertical-align: baseline;
  line-height: 20px;
}
.play-num span{
  padding-left: 2px;
}
ul.content {
  padding: 0 15px;
}
ul.content > li {
  display: inline-block; /**设置为行内元素 */
  width: 130px; /**宽度需要 */
  text-align: left;
  cursor: pointer;
}
</style>